package com.example.himalaya.adapters;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.himalaya.R;
import com.example.himalaya.views.PlayListPopupWindow;
import com.ximalaya.ting.android.opensdk.model.track.Track;

import java.util.ArrayList;
import java.util.List;

public class PopWindowsPlayListRvAdapter extends RecyclerView.Adapter<PopWindowsPlayListRvAdapter.ViewHolder> {
    private PlayListPopupWindow.PlayListItemClickListener mItemClickListener=null;

    public class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView mPlayListTrackTitle;
        private final ImageView mPlayIconIv;
        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            mPlayListTrackTitle = itemView.findViewById(R.id.play_list_track_title_tv);
            mPlayIconIv = itemView.findViewById(R.id.play_icon_iv);
        }
        public void setDate(Track track) {
            mPlayListTrackTitle.setText(track.getTrackTitle());
        }

    }



    private List<Track> mTracks = new ArrayList<>();
    private int playingIndex = 0;
    public void setCurrentPlayPosition(int index) {
        playingIndex = index;
        notifyDataSetChanged();
    }
    public void setOnItemClicker(PlayListPopupWindow.PlayListItemClickListener listener) {
        this.mItemClickListener = listener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void setData(List<Track> tracks) {
        mTracks.clear();
        mTracks.addAll(tracks);
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_play_list, parent, false);
        return new ViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        Track track = mTracks.get(position);
        holder.setDate(track);

        holder.mPlayIconIv.setVisibility(playingIndex==position?View.VISIBLE:View.GONE);
        holder.mPlayListTrackTitle.setTextColor(holder.itemView.getContext().getResources().getColor(playingIndex == position ? R.color.main_color : R.color.darkGray));

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mItemClickListener != null) {
                    mItemClickListener.onItemClick((Integer) holder.itemView.getTag());
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return mTracks.size();
    }

}
